package src.auxiliary 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	/**
	 * ...
	 * @author Morozov
	 * 
	 */
	public class KeyboardHandler extends Sprite
	{
		
		private var keys:Array;
		private var initialized :Boolean = false;
		
		public function KeyboardHandler() 
		{
			this.addEventListener(Event.ADDED_TO_STAGE, initialize);
		}
		
		private function initialize(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, initialize);
			
			keys = new Array();
			
			//Keyboard listeners:
			this.parent.stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
			this.parent.stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
			this.initialized = true;
		}
		
		private function onUp(e:KeyboardEvent):void 
		{
			this.keys[e.keyCode] = false;
		}
		
		private function onDown(e:KeyboardEvent):void 
		{
			this.keys[e.keyCode] = true;
		}
		
		public function keyDown(keyCode:int):Boolean
		{
			if (initialized)
			{
				return keys[keyCode];
			}
			else
				return false;
		}
		
	}

}